package com.seleniti.enpareja.web.appbase.client.ui.widget;

import org.ioc.commons.impl.gwt.client.ui.DisplayComposite;
import org.ioc.commons.ui.IsActivable;
import org.ioc.commons.ui.IsWidget;
import org.ioc.commons.utils.FormatterLogger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.DeckLayoutPanel;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel.Action;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel.Event;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel.Operation;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel.Presenter;

public class SliderPanelWidget extends DisplayComposite<Event, Operation, Action, Presenter> implements SliderPanel,
		AcceptsOneWidget {

	private static final FormatterLogger logger = FormatterLogger.getLogger(SliderPanelWidget.class);

	private static SliderPanelUiBinder uiBinder = GWT.create(SliderPanelUiBinder.class);

	interface SliderPanelUiBinder extends UiBinder<Widget, SliderPanelWidget> {
	}

	@UiField
	DeckLayoutPanel mainContainer;

	@UiField
	Panel withLogoPanelContainer;

	@UiField
	DeckPanel withLogoPanel;

	@UiField
	DeckLayoutPanel withoutLogoPanel;

	private BoardType boardType;

	private IsWidget theWidget;

	private Widget current;

	public SliderPanelWidget() {
		initWidget(uiBinder.createAndBindUi(this));
		this.setBoardType(BoardType.WITH_LOGO);
	}

	@Override
	public BoardType getBoardType() {
		return boardType;
	}

	@Override
	public void setBoardType(BoardType boardType) {
		this.boardType = boardType;
		logger.info("Setting board type: {0}", boardType);
		// mainContainer.showWidget(boardType.ordinal());
	}

	@Override
	public IsWidget getWidgetParent() {
		return DisplayComposite.getWidgetParent(this);
	}

	@Override
	public void setSingleWidget(IsWidget widget) {
		this.theWidget = widget;
		Widget w = (Widget) widget.asWidget();

		setSingleWidget(w);
	}

	public void setSingleWidget(Widget w) {
		withLogoPanel.setVisible(w != null);
		withoutLogoPanel.setVisible(w != null);

		if (w == null) {
			return;
		}

		mainContainer.showWidget(boardType.ordinal());

		switch (this.boardType) {
		case WITH_LOGO:
			if (withLogoPanel.getWidgetIndex(w) < 0) {
				withLogoPanel.add(w);
			}

			if (current != w) {
				if (current != null && current instanceof IsActivable && ((IsActivable) current).isActive()) {
					((IsActivable) current).deactivate();
				}
			}
			logger.info("Showing with logo view: {0}", w.getClass());
			withLogoPanel.showWidget(withLogoPanel.getWidgetIndex(w));

			if (current != w) {
				if (w instanceof IsActivable && !((IsActivable) w).isActive()) {
					((IsActivable) w).activate();
				}
			}

			break;
		case WITHOUT_LOGO:
			if (withoutLogoPanel.getWidgetIndex(w) < 0) {
				withoutLogoPanel.add(w);
			}

			if (current != w) {
				if (current != null && current instanceof IsActivable && ((IsActivable) current).isActive()) {
					((IsActivable) current).deactivate();
				}
			}
			logger.info("Showing without logo view: {0}", w.getClass());
			withoutLogoPanel.showWidget(w);
			if (current != w) {
				if (w instanceof IsActivable && !((IsActivable) w).isActive()) {
					((IsActivable) w).activate();
				}
			}
			break;
		}

		this.current = w;

	}

	@Override
	public IsWidget getSingleWidget() {
		return this.theWidget;
	}

	@Override
	public int getWidth() {
		return super.getOffsetWidth();
	}

	@Override
	public int getHeight() {
		return super.getOffsetHeight();
	}

	@Override
	public void setWidget(com.google.gwt.user.client.ui.IsWidget w) {
		if (w instanceof IsWidget) {
			this.theWidget = (IsWidget) w;
		}
		setSingleWidget(w != null ? w.asWidget() : null);
	}
}
